package com.dynamixsoftware.printershare;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build.VERSION;
import android.provider.Contacts.ContactMethods;
import android.provider.Contacts.Phones;
import android.provider.ContactsContract.Data;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.android.mms.util.SqliteWrapper;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ContactInfoCache {

	private static final Pattern EMAIL_ADDRESS_PATTERN = Pattern
			.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-]{1,256}\\@[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}(\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25})+");
	private static final Pattern NAME_ADDR_EMAIL_PATTERN = Pattern
			.compile("\\s*(\"[^\"]*\"|[^<>\"]+)\\s*<([^<>]+)>\\s*");
	private static final String PHONE_NUMBER_SEPARATORS = " ()-./";
	private static final Pattern QUOTED_STRING_PATTERN = Pattern.compile("\\s*\"([^\"]*)\"\\s*");
	private static final String SEPARATOR = ";";
	private final HashMap mCache = new HashMap();
	private String[] mContactInfoSelectionArgs = new String[1];
	private Context mContext;
	private String sLocalNumber;

	public ContactInfoCache(Context var1) {
		this.mContext = var1;
	}

	private static String extractAddrSpec(String var0) {
		Matcher var1 = NAME_ADDR_EMAIL_PATTERN.matcher(var0);
		if (var1.matches()) {
			var0 = var1.group(2);
		}

		return var0;
	}

	private static String filterPhoneNumber(String var0) {
		if (var0 == null) {
			return null;
		} else {
			int var1 = var0.length();
			StringBuilder var2 = new StringBuilder(var1);

			for (int var3 = 0; var3 < var1; ++var3) {
				char var4 = var0.charAt(var3);
				if (" ()-./".indexOf(var4) == -1) {
					var2.append(var4);
				}
			}

			return var2.toString();
		}
	}

	private String getCallerId(String var1) {
		ContactInfoCache.CacheEntry var2 = this.getContactInfo(var1);
		if (var2 != null && !TextUtils.isEmpty(var2.name)) {
			var1 = var2.name;
		}

		return var1;
	}

	private static String getEmailDisplayName(String var0) {
		Matcher var1 = QUOTED_STRING_PATTERN.matcher(var0);
		if (var1.matches()) {
			var0 = var1.group(1);
		}

		return var0;
	}

	private String getLocalNumber() {
		if (this.sLocalNumber == null) {
			this.sLocalNumber = ((TelephonyManager) this.mContext.getSystemService("phone")).getLine1Number();
		}

		return this.sLocalNumber;
	}

	private static boolean isEmailAddress(String var0) {
		if (TextUtils.isEmpty(var0)) {
			return false;
		} else {
			String var1 = extractAddrSpec(var0);
			return EMAIL_ADDRESS_PATTERN.matcher(var1).matches();
		}
	}

	private boolean isLocalNumber(String var1) {
		return PhoneNumberUtils.compare(var1, this.getLocalNumber());
	}

	private ContactInfoCache.CacheEntry queryContactInfoByNumber(String var1) {
		ContactInfoCache.CacheEntry var2 = new ContactInfoCache.CacheEntry();
		var2.phoneNumber = var1;
		this.mContactInfoSelectionArgs[0] = var1;
		Cursor var3 = this.mContext.getContentResolver().query(ContactInfoCache.Impl.CALLER_ID_CONTENT_URI,
				ContactInfoCache.Impl.CALLER_ID_PROJECTION, ContactInfoCache.Impl.CALLER_ID_SELECTION,
				this.mContactInfoSelectionArgs, (String) null);
		if (var3 != null) {
			try {
				if (var3.moveToFirst()) {
					var2.phoneNumber = var3.getString(0);
					var2.phoneLabel = var3.getString(1);
					var2.name = var3.getString(2);
					var2.person_id = var3.getLong(3);
				}
			} finally {
				var3.close();
			}
		}

		return var2;
	}

	private ContactInfoCache.CacheEntry queryEmailDisplayName(String var1) {
		ContactInfoCache.CacheEntry var2 = new ContactInfoCache.CacheEntry();
		this.mContactInfoSelectionArgs[0] = var1;
		Cursor var3 = SqliteWrapper.query(this.mContext, this.mContext.getContentResolver(),
				ContactInfoCache.Impl.CONTACT_METHOD_CONTENT_URI, ContactInfoCache.Impl.CONTACT_METHOD_PROJECTION,
				ContactInfoCache.Impl.CONTACT_METHOD_SELECTION, this.mContactInfoSelectionArgs, (String) null);
		if (var3 != null) {
			try {
				while (var3.moveToNext()) {
					var2.person_id = var3.getLong(1);
					String var5 = var3.getString(0);
					if (!TextUtils.isEmpty(var5)) {
						var2.name = var5;
					}
				}
			} finally {
				var3.close();
			}
		}

		return var2;
	}

	public ContactInfoCache.CacheEntry getContactInfo(String var1) {
		return isEmailAddress(var1) ? this.getContactInfoForEmailAddress(var1) : this
				.getContactInfoForPhoneNumber(var1);
	}

	public ContactInfoCache.CacheEntry getContactInfoForEmailAddress(String param1) {
		// $FF: Couldn't be decompiled
	}

	public ContactInfoCache.CacheEntry getContactInfoForPhoneNumber(String param1) {
		// $FF: Couldn't be decompiled
	}

	public String getContactName(String var1) {
		if (TextUtils.isEmpty(var1)) {
			return "";
		} else {
			StringBuilder var2 = new StringBuilder();
			String[] var3 = var1.split(";");
			int var4 = var3.length;

			for (int var5 = 0; var5 < var4; ++var5) {
				String var6 = var3[var5];
				if (var6.length() > 0) {
					var2.append(";");
					if (this.isLocalNumber(var6)) {
						var2.append("Me");
					} else if (isEmailAddress(var6)) {
						var2.append(this.getDisplayName(var6));
					} else {
						var2.append(this.getCallerId(var6));
					}
				}
			}

			if (var2.length() > 0) {
				return var2.substring(1);
			} else {
				return "";
			}
		}
	}

	public String getDisplayName(String var1) {
		Matcher var2 = NAME_ADDR_EMAIL_PATTERN.matcher(var1);
		if (var2.matches()) {
			var1 = getEmailDisplayName(var2.group(1));
		} else {
			ContactInfoCache.CacheEntry var3 = this.getContactInfoForEmailAddress(var1);
			if (var3 != null && var3.name != null) {
				return var3.name;
			}
		}

		return var1;
	}

	abstract static class Impl {

		static Uri CALLER_ID_CONTENT_URI;
		static String[] CALLER_ID_PROJECTION;
		static String CALLER_ID_SELECTION;
		static final int CONTACT_ID_COLUMN = 3;
		static Uri CONTACT_METHOD_CONTENT_URI;
		static final int CONTACT_METHOD_ID_COLUMN = 1;
		static final int CONTACT_METHOD_NAME_COLUMN = 0;
		static String[] CONTACT_METHOD_PROJECTION;
		static String CONTACT_METHOD_SELECTION;
		static final int CONTACT_NAME_COLUMN = 2;
		static final int PHONE_LABEL_COLUMN = 1;
		static final int PHONE_NUMBER_COLUMN;

		static {
			if (Integer.parseInt(VERSION.SDK) < 5) {
				ContactInfoCache.ImplOld.init();
			} else {
				ContactInfoCache.ImplNew.init();
			}
		}

	}

	private static class ImplNew extends ContactInfoCache.Impl {

		static void init() {
			CONTACT_METHOD_CONTENT_URI = Data.CONTENT_URI;
			CONTACT_METHOD_PROJECTION = new String[] { "data4", "contact_id" };
			CONTACT_METHOD_SELECTION = "data1=? AND mimetype=\'vnd.android.cursor.item/email_v2\'";
			CALLER_ID_CONTENT_URI = Data.CONTENT_URI;
			CALLER_ID_PROJECTION = new String[] { "data1", "data3", "display_name", "contact_id" };
			CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(data1,?) AND mimetype=\'vnd.android.cursor.item/phone_v2\'";
		}
	}

	private static class ImplOld extends ContactInfoCache.Impl {

		static void init() {
			CONTACT_METHOD_CONTENT_URI = ContactMethods.CONTENT_URI;
			CONTACT_METHOD_PROJECTION = new String[] { "display_name", "person" };
			CONTACT_METHOD_SELECTION = "data=?";
			CALLER_ID_CONTENT_URI = Phones.CONTENT_URI;
			CALLER_ID_PROJECTION = new String[] { "number", "label", "name", "person" };
			CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(number,?)";
		}
	}

	static class CacheEntry {

		public String name;
		public long person_id;
		public String phoneLabel;
		public String phoneNumber;

	}
}
